package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ware.annotation.Token;
import com.example.ware.model.po.Abnormal;
import com.example.ware.model.po.Admin;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.service.AbnormalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/abnormal")
public class AbnormalController {
    @Autowired
    AbnormalService abnormalService;

    @Resource
    RedisTemplate redisTemplate;


//    获取列表数据
    @Token
    @RequestMapping("/list")
    public ResponseData getList(Page p, String start, String end, @RequestHeader String token){

        Admin admin = (Admin)redisTemplate.opsForValue().get("Admin_" + token);
        String wid=admin.getWid();



        Page<Abnormal> abnormalPage=new Page<>();
        QueryWrapper<Abnormal> qw = new QueryWrapper<>();
        qw.eq("wid",wid);
        qw.orderByDesc("createtime");
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            qw.between("creattime", start, end); // 添加查询条件，筛选出在start和end之间的记录

           abnormalPage=abnormalService.page(p,qw);
        }else{

            abnormalPage=abnormalService.page(p,qw);
        }

        return new ResponseData(abnormalPage,true,"获取成功",200);

    }
}
